package com.app.buss.ui.mine;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.app.buss.R;
import com.app.buss.ui.AboutActivity;
import com.app.buss.ui.FavActivity;
import com.app.buss.ui.FeedBackActivity;
import com.app.buss.ui.MyTopicsActivity;
import com.app.buss.ui.PersonInfoActivity;
import com.app.buss.ui.SettingActivity;
import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

public class MineFragment extends Fragment {


    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.tv_name)
    TextView tvName;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_me, container, false);
        ButterKnife.bind(this,root);
        return root;
    }

    @OnClick({R.id.img_head, R.id.tv_my_fav, R.id.tv_my_topic, R.id.tv_feedback, R.id.tv_about, R.id.tv_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_head:
                startActivityForResult(new Intent(getActivity(), PersonInfoActivity.class),11);
                break;
            case R.id.tv_my_fav:
                startActivity(new Intent(getActivity(), FavActivity.class));
                break;
            case R.id.tv_my_topic:
                startActivity(new Intent(getActivity(), MyTopicsActivity.class));
                break;
            case R.id.tv_feedback:
                startActivity(new Intent(getActivity(), FeedBackActivity.class));
                break;
            case R.id.tv_about:
                startActivity(new Intent(getActivity(), AboutActivity.class));
                break;
            case R.id.tv_setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK && requestCode ==11){
            SharedPreferences sharedPreferences = getActivity().getSharedPreferences("person", getActivity().MODE_PRIVATE);
            String head = sharedPreferences.getString("head","");
            byte[] decodedString = Base64.decode(head, Base64.DEFAULT);
            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            Glide.with(getActivity()).load(decodedByte).into(imgHead);
            tvName.setText(sharedPreferences.getString("name","Hero123"));
        }
    }
}
